<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-mpChannelMethod-edit" th:object="${mpChannelMethod}">
            <input id="id" name="id" th:field="*{id}"  type="hidden">
			<div class="form-group">	
				<label class="col-sm-3 control-label">支付类型：</label>
				<div class="col-sm-8">
					<select class="form-control" id="payKindId" name="payKindId">
						<option value="" selected>----请选择----</option>
					</select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">钱包类型：</label>
				<div class="col-sm-8">
					<select class="form-control" id="walletKindId" name="walletKindId">
						<option value="" selected>----请选择----</option>
					</select>
				</div>
			</div>
			<input id="oldWalletKindId" name="oldWalletKindId" th:field="*{walletKindId}"  type="hidden">
			<div class="form-group">	
				<label class="col-sm-3 control-label">支付费率：</label>
				<div class="col-sm-8">
					<input id="payRate" name="payRate" placeholder="0.0001"  class="form-control" type="text" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">支付限额(元)：</label>
				<div class="col-sm-3" style="display:inline">
					<input id="payLimitLeft" name="payLimitLeft" class="form-control" type="text"  required>
				</div>
				<div style="display:inline;margin-left: -1%;margin-top: 1%;position: absolute;">
					<span>—</span>
				</div>
				<div class="col-sm-3">
					<input id="payLimitRight" name="payLimitRight" class="form-control" type="text" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
					<div class="onoffswitch">
						<input type="checkbox" th:checked="${mpChannelMethod.status == 0 ? true : false}" class="onoffswitch-checkbox" id="status" name="status">
						<label class="onoffswitch-label" for="status">
							<span class="onoffswitch-inner"></span>
							<span class="onoffswitch-switch"></span>
						</label>
					</div>
				</div>
			</div>
			<input id="channelId" name="channelId" th:field="*{channelId}"  type="hidden">
		</form>
    </div>
    <div th:include="include::footer"></div>
    <script th:inline="javascript">
        var prefix = ctx + "channel/method";
        var $from = $("#form-mpChannelMethod-edit");
		var mpChannelMethod = [[${mpChannelMethod}]];
        // 修改选项 赋值
		// 支付费率
        var payRate = mpChannelMethod.payRate.toFixed(4);
        $from.find("input[name='payRate']").val(payRate);
        // 支付限额左
        var payLimitLeft = new Decimal(mpChannelMethod.payLimitLeft).div(new Decimal(100));
        $from.find("input[name='payLimitLeft']").val(payLimitLeft);
        // 支付限额右
        var payLimitRight = new Decimal(mpChannelMethod.payLimitRight).div(new Decimal(100));
        $from.find("input[name='payLimitRight']").val(payLimitRight);
		// 旧-支付类型id
		var oldPayKindId = mpChannelMethod.payKindId;
        // 旧-钱包类型id
        var oldWalletKindId = mpChannelMethod.walletKindId;



		/*自定义校验规则*/
        $.validator.addMethod("regex",function (value, element, params) {
            var exp = new RegExp(params);
            return exp.test(value);
        },"请输入数字(可保留两位小数)");
        $.validator.addMethod("regex2",function (value, element, params) {
            var exp = new RegExp(params);
            return exp.test(value);
        },"小于1(可保留四位小数)");
        $.validator.addMethod("NoZero",function (value, element, params) {
            var exp = new RegExp(params);
            return exp.test(value);
        },"不能为零");
        /*规则校验*/
		$("#form-mpChannelMethod-edit").validate({
			rules:{
                payRate:{
                    regex2:"^[0]+(.[0-9]{4}|.[0-9]{3}|.[0-9]{2}|.[0-9]{1})?$",
                    NoZero:"^([^0]|.{2,})$",
                },
                payLimitLeft:{
                    regex:"^[0-9]+(.[0-9]{2}|.[0-9]{1})?$"
                },
                payLimitRight:{
                    regex:"^[0-9]+(.[0-9]{2}|.[0-9]{1})?$"
                }
			},
            messages: {
                "walletKindId": {
                    remote: "该渠道的钱包类型已经存在"
                }
            },
			focusCleanup: true
		});


        /*支付类型标识下拉框*/
        $.post(ctx + "basedata/paykind/list", {},function (response) {
            var data = response.rows;
            var option = "";
            for (var i = 0; i < data.length; i++) {
                if (mpChannelMethod.payKindId == data[i].id) {
                    option += "<option value='"+ data[i].id +"' selected='selected'>" + data[i].name + "</option>"
				}else {
                    option += "<option value='"+ data[i].id +"'>" + data[i].name + "</option>"
				}
            }
            $("#form-mpChannelMethod-edit").find("select[name='payKindId']").html("").append(option);
        });


        /*钱包类型标识下拉框*/
        $.post(ctx + "basedata/walletkind/list", {},function (response) {
            var data = response.rows;
            var option = "";
            for (var i = 0; i < data.length; i++) {
                if (mpChannelMethod.walletKindId == data[i].id){
                    option += "<option value='"+ data[i].id +"' selected='selected'>" + data[i].name + "</option>"
				}else {
                    option += "<option value='"+ data[i].id +"'>" + data[i].name + "</option>"
				}
            }
            $("#form-mpChannelMethod-edit").find("select[name='walletKindId']").html("").append(option);
        });

        /**
		 * 提交表单
         */
		function submitHandler() {
	        if ($.validate.form()) {
                var data = $("#form-mpChannelMethod-edit").serializeArray();
                var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
                if (status != 0){
                    data.push({"name": "status", "value": status});
                } else {
                    data[7].value = status;
                }

                // 支付费率
                // 支付限额左
                data[5].value = new Decimal(data[5].value).mul(new Decimal(100));
                // 支付限额右
                data[6].value = new Decimal(data[6].value).mul(new Decimal(100));

                data.push({"name": "oldPayKindId", "value": oldPayKindId});
                data.push({"name": "oldWalletKindId", "value": oldWalletKindId});

                $.operate.save(prefix + "/edit", data);
                // 判断钱包类型有没有发生改变，有则删除该渠道对应的钱包类型，并生成新的钱包类型
                /*var oldWalletKindId1 = $("input[id='oldWalletKindId']").val();
                $.operate.save(prefix + "/edit", data);
                if (oldWalletKindId1 != data[2].value) {
                    $.post(ctx + "channel/wallet/deleteByChannelIdAndWalletKindId", {'channelId':mpChannelMethod.channelId,'walletKindId':oldWalletKindId1},function (response) {
                    });
                }*/

	        }
	    }
	</script>
</body>
</html>
